package com.example.prom.qianduoduo.y2022.controller;

import com.alibaba.fastjson.JSON;
import com.example.prom.qianduoduo.y2022.model.ChictrExcelModel;
import com.example.prom.qianduoduo.y2022.model.ClinicaltrialsExcelModel;
import com.example.prom.strategy.excel.CustomCellStyleStrategy;
import com.example.prom.strategy.excel.CustomColumnWidthStrategy;
import com.example.prom.utils.DateUtil;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static com.alibaba.excel.EasyExcelFactory.write;

@RestController
@RequestMapping("chictr")
public class ChictrController {

    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

    private List<ChictrExcelModel> resultList = new ArrayList<>();
    /**
     * 1.检索注册题目
     * 2.点击页面，滑动滑块解锁文章
     * 3.获取cookie
     * 4.修改totalPage
     * @param request
     * @throws Exception
     * https://www.chictr.org.cn/
     * https://www.chictr.org.cn/searchproj.html
     * https://blog.csdn.net/jiangxwa/article/details/119217408 // jsoup article
     */
    @RequestMapping("searchArticle")
    public void searchArticle(HttpServletRequest request) throws Exception {
        AtomicInteger excelIndex = new AtomicInteger(1);
        List<ChictrExcelModel> dataList = new ArrayList<>();
        int totalPage = Integer.parseInt(request.getParameter("totalPage"));
        for (int i = 1; i <= totalPage; i++) {
            Map<String, String> requestParamMap = new HashMap<>();
            Map<String, String[]> parameterMap = request.getParameterMap();
            for (String s : parameterMap.keySet()) {
                requestParamMap.put(s, parameterMap.get(s)[0]);
                requestParamMap.put("page", i+"");
            }
            String urlPrefix = "https://www.chictr.org.cn/";
            String firstPage = "searchproj.html";
            Connection connect = Jsoup.connect(urlPrefix + firstPage);
            connect.header("Cookie", request.getHeader("cookie"));
//            connect.header("Referer", request.getHeader("Referer"));
            Connection.Response response = connect.data(requestParamMap).method(Connection.Method.GET).ignoreContentType(true).execute();
            Document document = response.parse();
            Element table1 = document.getElementsByClass("table1").get(0);
            Elements table1Trs = table1.select("tr");
            // 非的查找使用not选择器
            for (Element table1Tr : table1Trs) {
                Elements td_aList = table1Tr.select("td a");
                if (td_aList.size() == 0){
                    continue;
                }
                Element tableTrs_td_a = table1Tr.select("td a").get(1);
                Elements aLabel = tableTrs_td_a.select("a[href]");
                for (Element element : aLabel) {
                    String title = element.attr("title");
                    String href = element.attr("href");
                    System.out.println(href);
                    if (!StringUtils.isEmpty(title)){
                        ChictrExcelModel chictrExcelModel = new ChictrExcelModel();
                        dataList.add(chictrExcelModel);
                        chictrExcelModel.setNum(excelIndex.getAndIncrement());

                        String[] split = href.split("[?]");
                        requestParamMap.clear();
                        String key = split[1].split("=")[0];
                        String value = split[1].split("=")[1];
                        requestParamMap.put(key, value);
                        Connection showprojPageConn = Jsoup.connect(urlPrefix + split[0]);
                        showprojPageConn.header("Cookie", request.getHeader("cookie"));
//                        showprojPageConn.header("Referer", request.getHeader("Referer"));
                        Connection.Response showprojPageResp = showprojPageConn.data(requestParamMap).method(Connection.Method.GET).ignoreContentType(true).execute();
                        Document showprojDoc = showprojPageResp.parse();
                        Elements divs = showprojDoc.getElementsByClass("project-ms");
                        for (Element div : divs) {
                            Elements tbody_tr = div.select("tbody tr");
                            for (Element tbody_tr_ele : tbody_tr) {
                                Elements tbody_tr_ele_p = tbody_tr_ele.select("p.cn");
                                if (tbody_tr_ele_p != null && tbody_tr_ele_p.size() > 0){
                                    String p_text = tbody_tr_ele_p.first().text();
                                    if ("注册号：".equals(p_text)){
                                        String td_text = tbody_tr_ele.select("td").last().text().trim();
                                        chictrExcelModel.setRegNum(td_text);
                                        System.out.println(td_text.trim());
                                    } else if ("注册号状态：".equals(p_text)){
//                                        String td_text = tbody_tr_ele_p.get(1).text().trim();
                                        String td_text = tbody_tr_ele.select("td").last().text().trim();
                                        chictrExcelModel.setRegStatus(td_text);
                                    } else if ("注册时间：".equals(p_text)){
//                                        String td_text = tbody_tr_ele_p.get(1).text().trim();
                                        String td_text = tbody_tr_ele.select("td").last().text().trim();
                                        chictrExcelModel.setRegYear(td_text);
                                    } else if ("注册题目：".equals(p_text)) {
                                        String td_text = tbody_tr_ele_p.get(1).text().trim();
                                        chictrExcelModel.setReSearchTopic(td_text);
                                    } else if ("研究实施负责（组长）单位：".equals(p_text)) {
                                        String td_text = tbody_tr_ele_p.get(1).text().trim();
                                        chictrExcelModel.setReSearchUnit(td_text);
                                    } else if ("研究实施负责（组长）单位地址：".equals(p_text)) {
                                        String td_text = tbody_tr_ele_p.get(1).text().trim();
                                        chictrExcelModel.setReSearchUnitAddr(td_text);
                                    } else if ("试验主办单位(项目批准或申办者)：".equals(p_text)) {
//                                        Elements trs = tbody_tr_ele.select("tr");
//                                        for (Element tr : trs) {
//                                            Elements tr_tds = tr.select("td");
//                                            if (tr_tds != null && tr_tds.size() > 0){
//                                                if ("单位(医院)：".equals(tr_tds.get(0).text())){
//                                                    String unit = tr_tds.get(1).text().trim();
//                                                    String addr = tr_tds.get(3).text().trim();
//                                                    chictrExcelModel.setBiddingUnit(unit);
//                                                    chictrExcelModel.setBiddingUnitAddr(addr);
//                                                }
//                                            }
//                                        }
                                        Elements in_table_tbodys = tbody_tr_ele.select("table tbody");
                                        List<List<String>> trialSponsor= new ArrayList<>();
                                        for (Element in_table_tbody : in_table_tbodys) {
                                            Element tr_0 = in_table_tbody.select("tr").get(0);
                                            Elements select0 = tr_0.select("p:not(.cn)");
                                            List<String> collect0 = select0.stream().map(item -> item.text().trim()).collect(Collectors.toList());
                                            Element tr_1 = in_table_tbody.select("tr").get(2);
                                            Elements select1 = tr_1.select("p:not(.cn)");
                                            List<String> collect1 = select1.stream().map(item -> item.text().trim()).collect(Collectors.toList());
                                            collect0.addAll(collect1);
                                            trialSponsor.add(collect0);
                                        }
                                        chictrExcelModel.setTrialSponsor(JSON.toJSONString(trialSponsor));

                                    } else if ("研究疾病：".equals(p_text)) {
                                        String td_text = tbody_tr_ele_p.get(1).text().trim();
                                        chictrExcelModel.setResearchedDisease(td_text);
                                    } else if ("研究类型：".equals(p_text)) {
                                        String td_text = tbody_tr_ele_p.get(1).text().trim();
                                        chictrExcelModel.setResearchedDiseaseType(td_text);
                                    } else if ("研究所处阶段：".equals(p_text)) {
                                        String td_text = tbody_tr_ele.select("td span").text().trim();
                                        chictrExcelModel.setReSearchStage(td_text);
                                    } else if ("研究实施地点：".equals(p_text)) {
                                        Elements tables = tbody_tr_ele.select("table");
                                        List<Map<String, String>> measureList = new ArrayList<>();
                                        for (Element table : tables) {
                                            Elements trs = table.select("tr");
                                            Map<String, String> measureMap = new TreeMap<>();
                                            int m = 0;
                                            for (Element tr : trs) {
                                                m++;
                                                if (m == 1 || m == 3) {
                                                    String country = tr.select("td p.cn").first().text().trim().replace("：","");
                                                    String countryValue = tr.select("td").get(1).text().trim().replace("：","");
                                                    String province = tr.select("td p.cn").get(1).text().trim().replace("：","");
                                                    String provinceValue = tr.select("td").get(3).text().trim().replace("：","");
                                                    measureMap.put(country, countryValue);
                                                    measureMap.put(province, provinceValue);
                                                }
                                            }
                                            measureList.add(measureMap);
                                        }
                                        chictrExcelModel.setReSearchSite(JSON.toJSONString(measureList));
                                    } else if ("研究实施时间：".equals(p_text) || "研究执行时间：".equals(p_text)) {
                                        try {
                                            Elements td = tbody_tr_ele.select("td");
                                            String text = td.get(1).text();
                                            String text2 = td.get(3).text();
                                            chictrExcelModel.setResearchedTime(text2);
                                            Pattern pattern = Pattern.compile("(\\d{4}-\\d{2}-\\d{2}.\\d{2}:\\d{2}:\\d{2})");
                                            Matcher matcher = pattern.matcher(text);
                                            if (matcher.find()){
                                                String beginStr = matcher.group();
                                                boolean beginBool = DateUtil.compareTime(beginStr);
                                                if (Boolean.FALSE.equals(beginBool)) {
                                                    chictrExcelModel.setReSearchStatus("未开始 ( " + text + " ) ");
                                                } else if (matcher.find()) {
                                                    String endStr = matcher.group();
                                                    boolean endBool = DateUtil.compareTime(endStr);
                                                    if (Boolean.TRUE.equals(endBool)) {
                                                        chictrExcelModel.setReSearchStatus("已完成 ( " + text + " )");
                                                    } else {
                                                        chictrExcelModel.setReSearchStatus("进行中 ( " + text + " )");
                                                    }
                                                }
                                            }

                                        } catch (Exception e){
                                            e.printStackTrace();
                                        }


                                    } else if ("研究设计：".equals(p_text)) {
                                        String td_text = tbody_tr_ele_p.get(1).text().trim();
                                        chictrExcelModel.setReSearchDesign(td_text);
                                    } else if ("研究目的：".equals(p_text)) {
                                        String td_text = tbody_tr_ele_p.last().text().trim();
                                        chictrExcelModel.setReSearchPurpose(td_text);
                                    } else if ("干预措施：".equals(p_text)) {
                                        Elements tables = tbody_tr_ele.select("table");
                                        List<Map<String, String>> measureList = new ArrayList<>();
                                        for (int j = 0; j < tables.size(); j++) {
                                            Elements trs = tables.get(j).select("tr");
                                            Map<String, String> measureMap = new TreeMap<>();
                                            int m = 0;
                                            for (Element tr : trs) {
                                                m++;
                                                if (m == 1) {
                                                    String group = tr.select("td p.cn").get(0).text().trim().replace("：","");
                                                    String groupValue = tr.select("td p.cn").get(1).text().trim().replace("：","");
                                                    String sampleCount = tr.select("td p.cn").get(2).text().trim().replace("：","");
                                                    String sampleCountValue = tr.select("td").get(3).text().trim().replace("：","");
                                                    measureMap.put(group, groupValue);
                                                    measureMap.put(sampleCount, sampleCountValue);
                                                }

                                                if (m == 3){
                                                    String group = tr.select("td p.cn").get(0).text().trim().replace("：","");
                                                    String groupValue = tr.select("td p.cn").get(1).text().trim().replace("：","");
                                                    measureMap.put(group, groupValue);
                                                }

                                            }
                                            measureList.add(measureMap);
//                                            String text = tr_td1.get(8).text();
//                                            String count = tr_td1.get(3).text();
//                                            if (j % 2 == 0){
//                                                chictrExcelModel.setTestCapacity(count);
//                                                chictrExcelModel.setTestMeasure(text);
//                                            } else {
//                                                chictrExcelModel.setContrastCapacity(count);
//                                                chictrExcelModel.setContrastMeasure(text);
//                                            }
                                        }
                                        if (!CollectionUtils.isEmpty(measureList)) {
                                            chictrExcelModel.setInterveningMeasure(JSON.toJSONString(measureList));
                                        }
                                    } else if ("研究课题的正式科学名称：".equals(p_text)){
                                        String td_text = tbody_tr_ele_p.last().text().trim();
                                        chictrExcelModel.setReSearchTopicOfficialName(td_text);
                                    } else if ("申请注册联系人：".equals(p_text)) {
                                        String td_text = tbody_tr_ele_p.get(1).text().trim();
                                        chictrExcelModel.setRegistrationPerson(td_text);
                                    } else if ("申请人所在单位：".equals(p_text)) {
                                        String td_text = tbody_tr_ele_p.get(1).text().trim();
                                        chictrExcelModel.setRegistrationPersonDepartment(td_text);
                                    } else if ("经费或物资来源：".equals(p_text)) {
                                        String td_text = tbody_tr_ele_p.last().text().trim();
                                        chictrExcelModel.setSourceOfFundsOrMaterials(td_text);
                                    } else if ("纳入标准：".equals(p_text)) {
                                        String td_text = tbody_tr_ele_p.last().text().trim();
                                        String replace = td_text.replace("<br>", String.valueOf((char)10)); // String.valueOf((char)10)单元格内换行
                                        chictrExcelModel.setInclusionCriteria(replace);
                                    } else if ("排除标准：".equals(p_text)) {
                                        String td_text = tbody_tr_ele_p.last().text().trim();
                                        String replace = td_text.replace("<br>", String.valueOf((char)10)); // String.valueOf((char)10)单元格内换行
                                        chictrExcelModel.setExclusionCriteria(replace);
                                    } else if ("测量指标：".equals(p_text)) {
                                        Elements inner_trs = tbody_tr_ele.select("td").get(1).select("table tbody tr");
                                        List<String> names = new ArrayList<>();
                                        List<String> types = new ArrayList<>();
                                        List<String> times = new ArrayList<>();
                                        for (Element inner_tr : inner_trs) {
                                            Elements p_cn_select = inner_tr.select("p.cn");
                                            if (p_cn_select != null && p_cn_select.size() > 0) {
                                                String inner_trs_p_text = inner_tr.select("p.cn").first().text().trim();
                                                if ("指标中文名：".equals(inner_trs_p_text)) {
                                                    String inner_trs_p_text_in = inner_tr.select("p.cn").get(1).text().trim();
                                                    names.add(inner_trs_p_text_in);
                                                    // 指标类型
                                                    String inner_trs_p_text_in3 = inner_tr.select("p.cn").get(3).text().trim();
                                                    types.add(inner_trs_p_text_in3);
                                                }
//                                            if ("指标类型：".equals(inner_trs_p_text)) {
//
//                                            }
                                                if ("测量时间点：".equals(inner_trs_p_text)) {
                                                    String inner_trs_p_text_in = inner_tr.select("p.cn").get(1).text().trim();
                                                    times.add(inner_trs_p_text_in);
                                                }
                                            }
                                        }
                                        chictrExcelModel.setIndicatorsChineseName(JSON.toJSONString(names));
                                        chictrExcelModel.setIndicatorsType(JSON.toJSONString(types));
                                        chictrExcelModel.setIndicatorsMeasureTime(JSON.toJSONString(times));
//                                        chictrExcelModel.setFinalNorm(td_text);
                                    }
//                                    else if ("测量时间点：".equals(p_text)) {
//                                        String td_text = tbody_tr_ele_p.get(1).text().trim();
//                                        chictrExcelModel.setIndicatorsMeasureTime(td_text);
//                                    } else if ("指标类型：".equals(p_text)) {
//                                        String td_text = tbody_tr_ele_p.get(3).text().trim();
//                                        chictrExcelModel.setIndicatorsType(td_text);
//                                    }
                                    else if ("采集人体标本：".equals(p_text)){
                                        Elements inner_trs = tbody_tr_ele.select("td").get(1).select("table tbody tr");
                                        for (Element inner_tr : inner_trs) {
                                            Elements p_cn_select = inner_tr.select("p.cn");
                                            if (p_cn_select.size() > 0) {
                                                String p_text_p_text = inner_tr.select("p.cn").first().text().trim();
                                                if ("标本中文名：".equals(p_text_p_text)) {
                                                    String td_text = tbody_tr_ele.select("td").get(1).select("table tbody tr p.cn").get(1).text().trim();
                                                    chictrExcelModel.setSpecimenChineseName(td_text);
                                                }
                                            }
                                        }
                                    } else if ("征募研究对象情况：".equals(p_text)) {
                                        Elements inner_table_tbody_tr = tbody_tr_ele.select("table tbody tr");
//                                        for (Element innerTr : inner_table_tbody_tr) {
                                            Element minAgeEle = inner_table_tbody_tr.first().select("td").get(1);
                                            String minAge = minAgeEle.text().trim();
                                            Element maxAgeEle = inner_table_tbody_tr.last().select("td").get(1);
                                            String maxAge = maxAgeEle.text().trim();
                                            String age = minAge + "-" + maxAge + "岁";
                                            if (StringUtils.isEmpty(maxAge) && StringUtils.isEmpty(minAge)){
                                                // do nothing
                                            } else {
                                                chictrExcelModel.setResearchedAgeRange(age);
                                            }
//                                        }
                                    } else if ("性别：".equals(p_text)) {
                                        String td_text = tbody_tr_ele_p.get(1).text().trim();
                                        chictrExcelModel.setResearchedSex(td_text);
                                    } else if ("随机方法（请说明由何人用什么方法产生随机序列）：".equals(p_text)) {
                                        String td_text = tbody_tr_ele_p.get(1).text().trim();
                                        chictrExcelModel.setRandomMethod(td_text);
                                    } else if ("盲法：".equals(p_text)) {
                                        String td_text = tbody_tr_ele_p.get(1).text().trim();
                                        chictrExcelModel.setBlindMethod(td_text);
                                    }
                                }
                            }
                        }
                        chictrExcelModel.setDatetime(simpleDateFormat.format(new Date()));
                    }
                }
            }

            Thread.sleep(1000);
        }

        System.out.println("本次爬取条数："+dataList.size());
        resultList.addAll(dataList);
        System.out.println("总条数："+resultList.size());
    }

    @GetMapping("exportExcel")
    public void exportExcel() {
        System.out.println("去重前总数量："+resultList.size());
        ArrayList<ChictrExcelModel> removeDup = resultList.stream().collect(Collectors.collectingAndThen(
                Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(ChictrExcelModel::getRegNum))), ArrayList::new));
        System.out.println("去重后总数量："+removeDup.size());
        for (int i = 1; i <= removeDup.size(); i++) {
            ChictrExcelModel clinicaltrialsExcelModel = removeDup.get(i - 1);
            clinicaltrialsExcelModel.setNum(i);
        }
        String fileName = "C:\\Users\\hp\\Desktop\\钱多多\\0528.xlsx";
        write(fileName, ChictrExcelModel.class).sheet().head(ChictrExcelModel.class)
                .registerWriteHandler(new CustomColumnWidthStrategy())
                .registerWriteHandler(new CustomCellStyleStrategy()).doWrite(removeDup);
        System.out.println("生成excel成功");
    }
}
